<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on">
      <div class="title-container">
        <h3 class="title">云启课堂后台管理系统</h3>
      </div>

      <el-form-item prop="username" class="input-item">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
          class="custom-input"
        />
      </el-form-item>

      <el-form-item prop="password" class="input-item">
        <span class="svg-container">
          <svg-icon icon-class="password"/>
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
          class="custom-input"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        class="login-btn"
        @click.native.prevent="handleLogin"
      >登 录
      </el-button>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: '111111'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss">
$bg: #f6f6f6;
$dark_gray: #889aa4;
$light_gray: #333;
$cursor: #333;
$border-color: #dcdfe6;
$focus-color: #409EFF;
$hover-color: #c0c4cc;

.login-container {
  .el-input {
    display: inline-block;
    height: 48px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 48px;
      caret-color: $cursor;
      letter-spacing: 0.5px;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }

      &[type="password"] {
        font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", Arial, sans-serif;
        letter-spacing: 2px;
        font-size: 16px;
      }

      &:focus {
        outline: none;
      }
    }

    &.is-focus .el-input__inner {
      box-shadow: none;
    }
  }

  .el-form-item {
    border: 1px solid $border-color;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 5px;
    color: #454545;
    margin-bottom: 24px;
    transition: border-color 0.2s;

    &:hover {
      border-color: $hover-color;
    }

    &.is-focus,
    &.is-focusing,
    &:focus-within,
    &:active {
      border: none;
      border-color: $focus-color !important;
      box-shadow: 0 0 0 1px $focus-color !important;
    }
  }
}
</style>

<style lang="scss" scoped>
$dark_gray: #889aa4;
$light_gray: #333;

.login-container::before {
  content: '';
  background-image: url('~@/assets/svg/t2.svg');
  background-size: contain;
  background-repeat: no-repeat;
  position: absolute;
  top: 380px;
  left: 260px;
  width: 400px;
  height: 300px;
  z-index: 1;
}

.login-container {
  position: relative;
  min-height: 100vh;
  width: 100%;
  background-color: white;
  overflow: hidden;
  background-image: url("~@/assets/login/bg.png");
  background-size: 80% 100%;
  background-position: left bottom;
  background-repeat: no-repeat;
  background-attachment: fixed;

  .login-form {
    position: absolute;
    top: 50%;
    right: 350px;
    //transform: translateY(-50%);
    width: 420px;
    max-width: 100%;
    padding: 40px 35px 30px;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
    opacity: 0;
    transform: translateY(-50%) translateX(40px);
    animation: fade-slide-in 0.6s ease forwards;
  }

  @keyframes fade-slide-in {
    to {
      opacity: 1;
      transform: translateY(-50%) translateX(0);
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    margin-bottom: 30px;

    .title {
      font-size: 22px;
      color: $light_gray;
      margin: 0 auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .login-btn {
    width: 100%;
    height: 48px;
    margin-top: 10px;
    font-size: 16px;
    letter-spacing: 4px;
    transition: all 0.3s;

    &:hover {
      opacity: 0.9;
    }
  }
}
</style>
